// CoalMineInfoQueryHandler.cpp: implementation of the CoalMineInfoQueryHandler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kwservercore.h"
#include "CoalMineInfoQueryHandler.h"
#include "Server.h"

#include "CommonLib\Packet.h"
#include "CommonLib\Structure.h"

#include "CommServerLib\CommWithClient.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CoalMineInfoQueryHandler::CoalMineInfoQueryHandler(const Packet& packet, HANDLE hClient, bool bEClient ):
	BaseRequestHandler(packet, hClient)
{
	bEflongClient = bEClient;
}

CoalMineInfoQueryHandler::~CoalMineInfoQueryHandler()
{
}

bool CoalMineInfoQueryHandler::Prehandling()
{
	if(bEflongClient){
		PrehandlingEx();
	}else{
		DWORD dwNumOfCoalMines = Server::Instance()->GetCoalMineUnits().size();
		DWORD dwBufSize;
		if (dwNumOfCoalMines > 0)
		{
			dwBufSize = sizeof(CoalMineInfoStruct)+sizeof(CoalMineInfoItem)*(dwNumOfCoalMines-1);
		}
		else
		{
			dwBufSize = sizeof(CoalMineInfoStruct);
		}
		CoalMineInfoStruct * pCoalMineInfoStruct = (CoalMineInfoStruct *)new BYTE[dwBufSize];
		pCoalMineInfoStruct->m_dwNumOfItems = dwNumOfCoalMines;
		pCoalMineInfoStruct->m_dwBufSize = dwBufSize;
		Server::Instance()->ConvertCoalMineUnitsToStruct(pCoalMineInfoStruct->m_CoalMineInfoItems, pCoalMineInfoStruct->m_dwNumOfItems);
		pCoalMineInfoStruct->m_nMethodAck = VCS_MR_OK;

		Packet packetAck(dwBufSize, (LPBYTE)pCoalMineInfoStruct);
		packetAck.m_dwId = m_pPacket->m_dwId;

		CommWithClient::Instance()->SendTo(packetAck, m_hClient);

		delete pCoalMineInfoStruct;

		return true;
	}
}
bool CoalMineInfoQueryHandler::PrehandlingEx()
{
	DWORD dwNumOfCoalMines = Server::Instance()->GetCoalMineUnits().size();
	DWORD dwBufSize;
	if (dwNumOfCoalMines > 0)
	{
		dwBufSize = sizeof(CoalMineInfoItemEx)+sizeof(CoalMineInfoItemEx)*(dwNumOfCoalMines-1);
	}
	else
	{
		dwBufSize = sizeof(CoalMineInfoItemEx);
	}
	CoalMineInfoStructEx * pCoalMineInfoStruct = (CoalMineInfoStructEx *)new BYTE[dwBufSize];
	pCoalMineInfoStruct->m_dwNumOfItems = dwNumOfCoalMines;
	pCoalMineInfoStruct->m_dwBufSize = dwBufSize;
	Server::Instance()->ConvertCoalMineUnitsToStruct(pCoalMineInfoStruct->m_CoalMineInfoItems, pCoalMineInfoStruct->m_dwNumOfItems);
	pCoalMineInfoStruct->m_nMethodAck = VCS_MR_OK;

	Packet packetAck(dwBufSize, (LPBYTE)pCoalMineInfoStruct);
	packetAck.m_dwId = m_pPacket->m_dwId;

	CommWithClient::Instance()->SendTo(packetAck, m_hClient);

	//delete pCoalMineInfoStruct;

	return true;
}